package com.twu.biblioteca.domain;


public class Movie implements Withdrawable {
    private final String id;
    private final String name;
    private final String director;
    private final int year;
    private final Integer rating;

    public Movie(String id, String name, String author, int year, Integer rating) {
        boolean ratingIsNullOrWithinBounds = rating != null && (rating.compareTo(10) > 0 || rating.compareTo(0) < 1);

        if(ratingIsNullOrWithinBounds){
            throw new RatingOutOfBoundsException();
        }

        this.id = id;
        this.name = name;
        this.director = author;
        this.year = year;
        this.rating = rating;
    }


    public Movie(String id, String name, String author, int year) {
        this(id, name, author, year, null);
    }

    @Override
    public String toString() {
        return String.format("%s - %s. %s. %d. %s.",
                id,
                name,
                director,
                year,
                printRating());
    }

    private String printRating() {
        if(this.rating == null) {
            return "Unrated";
        }
        return "Rating: " + this.rating;
    }

    @Override
    public boolean equals(Object o){
        Movie otherMovie = (Movie) o;
        return otherMovie.name.equals(this.name) &&
                otherMovie.director.equals(this.director) &&
                otherMovie.year == this.year;

    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public int compareTo(Object o) {
        Movie otherMovie = (Movie) o;
        return this.id.compareTo(otherMovie.getId());
    }


    @Override
    public void setWithdrawn(LibraryUser user) {

    }

    @Override
    public void setReturned() {

    }
}
